/**
 * FadePlugs -- 基于原生js的鼠标移动效果插件
 * @author 改着名儿玩
 */

/**
 * 插件核心方法
 * @param elements 输入元素
 */
var FadePlugs = function(elements){
	var HEIGHT = document.body.clientHeight;
	var WIDTH = document.body.clientWidth;
	var elements = elements;
	var ax = new Array();	//纵向速度的加速度
	var ay = new Array();	//
	var limitFloats = new Array();
	var baseX=new Array();
	var baseY=new Array();
	var directionX=new Array();
	var directionY=new Array();
	var mouseX=WIDTH/2;
	var mouseY=HEIGHT/2;
	
	window.onmousemove=function(event){
		mouseX = event.clientX;
		mouseY = event.clientY;
	}

	function init(elements){
		this.elements=elements;
		for(var i=0;i<elements.length;i++){
			baseX[i] = this.elements[i].offsetLeft;
			baseY[i] = this.elements[i].offsetTop;
			ax[i] = Math.abs(this.elements[i].getAttribute("data-ax"));
			ay[i] = Math.abs(this.elements[i].getAttribute("data-ay"));
			limitFloats[i] = this.elements[i].getAttribute("data-limit");
			directionX[i] = this.elements[i].getAttribute("data-ax")<0?-1:1;
			directionY[i] = this.elements[i].getAttribute("data-ay")<0?-1:1;
		}
		run();
	}

	function step(){
		for(var i=0;i<elements.length;i++){
			var mx = (directionX[i]*(mouseX-baseX[i])*limitFloats[i]/WIDTH - elements[i].offsetLeft + baseX[i]) * ax[i] / 100;
			var my = (directionY[i]*(mouseY-baseY[i])*limitFloats[i]/HEIGHT - elements[i].offsetTop + baseY[i]) * ay[i] / 100;
			elements[i].style.top = (elements[i].offsetTop + my) + "px";
			elements[i].style.left = (elements[i].offsetLeft + mx) + "px";
		}
		window.raf = requestAnimationFrame(step);				
	}
	function run(){
		window.requestAnimationFrame (step);
	}
	
	init(elements);
	run();
}